package com.elf.zero.datetime;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.NumberPicker;

import com.elf.zero.app.AbsLinearLayout;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

/**
 * 时间选择器
 * Created by Lidong on 2018/12/6.
 */
public class TimePicker extends AbsLinearLayout {

    public final static String format_time = "HH:mm";
    public final static String format_time2 = "HH:mm:ss";

    private NumberPicker np_hour, np_minute, np_second;

    public TimePicker(Context context) {
        super(context);
    }

    public TimePicker(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int getLayoutResId() {
        return 0;
    }

    @Override
    protected void initView(AttributeSet attrs) {
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);

        np_hour = new NumberPicker(getContext());
        np_minute = new NumberPicker(getContext());
        np_second = new NumberPicker(getContext());
        np_second.setVisibility(GONE);

        np_hour.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
        np_minute.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
        np_second.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);

        addView(np_hour);
        addView(new View(getContext()), 10, 1);
        addView(np_minute);
        addView(new View(getContext()), 10, 1);
        addView(np_second);

        initHour();
        initMinute();
        initSecond();

        setTime(Calendar.getInstance());
    }

    public void setShowSecond(boolean b) {
        np_second.setVisibility(b ? VISIBLE : GONE);
    }

    public void setTime(Calendar calendar) {
        setTime(calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE),
                calendar.get(Calendar.SECOND));
    }

    public void setTime(int hour, int minute, int second) {
        np_hour.setValue(hour);
        np_minute.setValue(minute);
        np_second.setValue(second);
    }

    public Calendar getTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, np_hour.getValue());
        calendar.set(Calendar.MINUTE, np_minute.getValue());
        calendar.set(Calendar.SECOND, np_second.getValue());
        return calendar;
    }

    public String getTime(String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA);
        return dateFormat.format(getTime().getTime());
    }

    private void initHour() {
        np_hour.setMinValue(0);
        np_hour.setMaxValue(23);
        String[] displayValues = new String[24];
        for (int i = 0; i < displayValues.length; i++) {
            displayValues[i] = i + "时";
        }
        np_hour.setDisplayedValues(displayValues);
    }

    private void initMinute() {
        np_minute.setMinValue(0);
        np_minute.setMaxValue(59);
        String[] displayValues = new String[60];
        for (int i = 0; i < displayValues.length; i++) {
            displayValues[i] = i + "分";
        }
        np_minute.setDisplayedValues(displayValues);
    }

    private void initSecond() {
        np_second.setMinValue(0);
        np_second.setMaxValue(59);
        String[] displayValues = new String[60];
        for (int i = 0; i < displayValues.length; i++) {
            displayValues[i] = i + "秒";
        }
        np_second.setDisplayedValues(displayValues);
    }
}
